home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / strpchrt.zip / EX_TEST.C < prev    next >
Text File  |  1993-07-10  |  7KB  |  268 lines

  1. /*
  2.    Strip Chart Library Test Example.
  3.    This program demonstrates the capabilities of the Library.
  4.  
  5.    Copyright (c) 1993 by Christopher Lim.  All rights reserved.
  6. */
  7.  
  8. /*
  9.    Function func() simulates digitizing a sine wave.
  10.    SPS defined below is the simulated sampling rate.
  11.    The program can be made to run faster by using a lower sampling rate.
  12. */
  13. #define SPS 300.0
  14.  
  15. #include "sc.h"
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <malloc.h>
  21. #ifdef BORLAND
  22. #include <alloc.h> /* for coreleft() */
  23. #endif
  24.  
  25. /*------------------------------------------------------------*/
  26.  
  27. int FreeMem(void)
  28. {
  29.    /* Prints out free memory.
  30.       This function is compiler dependent.
  31.     */
  32. #ifdef BORLAND
  33.     printf("Free memory: %ld\n",(unsigned long)coreleft());
  34. #endif
  35. #ifdef MICROSOFT
  36.    printf("Free memory: %ld\n",(unsigned long)_memavl());
  37. #endif
  38.     return 0;
  39. }
  40.  
  41. /*------------------------------------------------------------*/
  42.  
  43. double func(double t)
  44. {
  45.     /* Test function response for time t */
  46.     double x;
  47.     double amplitude = 0.5;
  48.     double period = 0.5;
  49.     x = amplitude * sin(2 * 3.1416 * t/period);
  50.     return(x);
  51. }
  52.  
  53. /*------------------------------------------------------------*/
  54.  
  55. char *Label(SC_WINDOW *w, double seconds, unsigned long labelCount)
  56. {
  57.    static char buf[80];
  58.    static int minutes = 0;
  59.    static int hours = 0;
  60.  
  61.    while (seconds >= 60.0)
  62.    {
  63.       seconds -= 60.0;
  64.       minutes++;
  65.    }
  66.    if (minutes >= 60)
  67.    {
  68.       minutes %= 60;
  69.       hours += minutes/60;
  70.    }
  71.    sprintf(buf,"%02d:%02d:%05.2f (%ld)",hours,minutes,seconds,
  72.       labelCount);
  73.    return buf;
  74. }
  75.  
  76. /*------------------------------------------------------------*/
  77.  
  78. void GetPrinterType(unsigned char *printerType, unsigned char *printerRes,
  79.    char **s)
  80. {
  81.    int c;
  82.    unsigned char i,j;
  83.  
  84.    printf("Select printer type and resolution code: \n");
  85.    printf("[1] 9 PIN LOW RES\n");
  86.    printf("[2] 9 PIN HIGH RES\n");
  87.    printf("[3] 24 PIN LOW RES\n");
  88.    printf("[4] 24 PIN HIGH RES\n");
  89.    printf("Select code [1,2,3 or 4]: ");
  90.    c = getch();
  91.    printf("%c\n",c);
  92.    switch (c)
  93.    {
  94.         case '1': i = SC_9PIN; j = SC_LOW_RES;
  95.              *s = "9 PIN LOW RES";
  96.              break;
  97.         case '2': i = SC_9PIN; j = SC_HIGH_RES;
  98.              *s = "9 PIN HIGH RES";
  99.              break;
  100.         case '3': i = SC_24PIN; j = SC_LOW_RES;
  101.              *s = "24 PIN LOW RES";
  102.              break;
  103.         case '4': i = SC_24PIN; j = SC_HIGH_RES;
  104.              *s = "24 PIN HIGH RES";
  105.              break;
  106.         default:
  107.              printf("Aborted\n");
  108.              exit(1);
  109.    }
  110.    printf("%s\n",*s);
  111.    *printerType = i;
  112.    *printerRes = j;
  113.    return;
  114. }
  115.  
  116. /*------------------------------------------------------------*/
  117.  
  118. void ErrorTrap(char *msg)
  119. {
  120.     printf("%s\n",msg);
  121.     exit(1);
  122. }
  123.  
  124. /*------------------------------------------------------------*/
  125.  
  126. void main()
  127. {
  128.    int i,j;
  129.    int error;
  130.    double scan;
  131.    double a;
  132.    SC_WINDOW *w1,*w2,*w3;
  133.    SC_WINDOW *text, *text1, *text2, *text3, *text4, *text5;
  134.    SC_WINDOW *text6, *text7, *text8;
  135.    SC_WINDOW *time;
  136.    SC_WINDOW *event1, *event2, *event3;
  137.    double sps = SPS; /* simulated sampling rate */
  138.    double t;
  139.    unsigned char printerType, printerRes;
  140.    static char string[128];
  141.    char *s;
  142.  
  143.    if (!SC_PrinterReady(LPT1))
  144.    {
  145.       printf("Printer at LPT1 is not ready!\n");
  146.       exit(1);
  147.    }
  148.  
  149.    GetPrinterType(&printerType,&printerRes,&s);
  150.  
  151.    FreeMem();
  152.  
  153.    /* Error trap */
  154.    SC_SetErrorTrap(ErrorTrap);
  155.  
  156.    /* Initialize: 8.5 inch paper, plot speed = 1 inch per second */
  157.    SC_Init(LPT1, printerType,printerRes,INCHES, 8.5, 1.0,&error);
  158.  
  159.    /* Horizontal Grids: 4 divisions */
  160.    SC_SetHorizontalGrids(4,&error);
  161.  
  162.    /* Time grids every 1 second */
  163.    SC_SetTimeGrids(1.0,&error);
  164.  
  165.    /* Format of Y labels */
  166.    SC_SetYlabels(10.0/72.0,"%+0.1f",3.0, &error);
  167.  
  168.    /* Text window to display title */
  169.    text = SC_AddTextWindow(0.5,14.0/72.0,&error);
  170.  
  171.    /* Add time axis at the top of the plot */
  172.    time = SC_AddTimeWindow(SC_yoffset+0.125,20.0/72.0, LABELS_ABOVE_AXIS,
  173.       2.0,5,Label,&error);
  174.  
  175.    /* Add trace windows */
  176.    text1 = SC_AddTextWindow(SC_yoffset+0.125,10.0/72.0,&error);
  177.    w1 = SC_AddTraceWindow(SC_yoffset+0.125, 1.0,
  178.      -1.0,1.0,SCF_BORDER|SCF_GRID|SCF_YLABEL,&error);
  179.  
  180.    text2 = SC_AddTextWindow(SC_yoffset+0.125,10.0/72.0,&error);
  181.    w2 = SC_AddTraceWindow(SC_yoffset+0.125, 1.0,
  182.      -1.0,1.0,SCF_BORDER|SCF_GRID|SCF_YLABEL,&error);
  183.  
  184.    text3 = SC_AddTextWindow(SC_yoffset+0.125,10.0/72.0,&error);
  185.    w3 = SC_AddTraceWindow(SC_yoffset+0.125, 1.0,
  186.      -1.0,1.0,SCF_CLIP|SCF_BORDER|SCF_GRID|SCF_YLABEL,&error);
  187.  
  188.    /* Add event windows */
  189.    text4 = SC_AddTextWindow(SC_yoffset+0.125,10.0/72.0,&error);
  190.    event1 = SC_AddEventWindow(SC_yoffset+0.125,0.125,
  191.       SCM_PULSE,0,&error);
  192.    event2 = SC_AddEventWindow(SC_yoffset+0.125,0.125,
  193.       SCM_TICK,0,&error);
  194.    event3 = SC_AddEventWindow(SC_yoffset+0.125,0.125,
  195.       SCM_BAR,HALF_FILL,&error);
  196.  
  197.    /* Add time axis at the bottom of the plot */
  198.    time = SC_AddTimeWindow(SC_yoffset+0.125,20.0/72.0, LABELS_BELOW_AXIS,
  199.       2.0,5,Label,&error);
  200.  
  201.    /* Add text window to display character set */
  202.    text5 = SC_AddTextWindow(SC_yoffset+0.25,12.0/72.0,&error);
  203.    text6 = SC_AddTextWindow(SC_yoffset+0.125,12.0/72.0,&error);
  204.    text7 = SC_AddTextWindow(SC_yoffset+0.125,10.0/72.0,&error);
  205.    text8 = SC_AddTextWindow(SC_yoffset+0.125,8.0/72.0,&error);
  206.  
  207.    /* initialize text windows */
  208.    sprintf(string,"STRIP CHART LIBRARY EXAMPLES [%s]",s);
  209.    SC_Text(text,string,&error);
  210.    SC_Text(text1,"Trace Window with gain ranging:",&error);
  211.    SC_Text(text2,"Trace Window with no clipping:",&error);
  212.    SC_Text(text3,"Trace Window with clipping:",&error);
  213.    SC_Text(text4,"Event Windows:",&error);
  214.  
  215.    /* Draw character set */
  216.    for (i=33,j=0; i < 128; i++,j++)
  217.       string[j] = i;
  218.    string[j] = 0; /* terminating NULL */
  219.    SC_Text(text5,string,&error);
  220.    SC_Text(text6,"Text Window: Scalable fonts", &error);
  221.    SC_Text(text7,"Text Window: Scalable fonts", &error);
  222.    SC_Text(text8,"Text Window: Scalable fonts",&error);
  223.  
  224.    /* draw traces */
  225.    a = 1.0; /* initial amplitude */
  226.    for (t=0.0; t < 9.5; t += 1.0/sps)
  227.    {
  228.       if (kbhit())
  229.       {
  230.          printf("User aborted.\n");
  231.          break;
  232.       }
  233.  
  234.       if (t >= 3.0 && t < 6.0 && a == 1.0)
  235.       { /* Quadruple the amplitude at 2.0 <= t < 4.0 */
  236.          a = 4.0;
  237.          SC_SetTraceRange(w1,-2.0,2.0,&error);
  238.          SC_Text(text1,"4 x Amplitude",&error);
  239.       }
  240.  
  241.       if (t >= 6.0 && a == 4.0)
  242.       { /* 1/4 the amplitude at t >= 4.0 */
  243.          a = 1.0;
  244.          SC_SetTraceRange(w1,-1.0,1.0,&error);
  245.          SC_Text(text1,"1/4 x Amplitude",&error);
  246.       }
  247.  
  248.       scan = a * func(t);
  249.  
  250.       SC_Trace(w1,scan,&error);
  251.       SC_Trace(w2,scan,&error);
  252.       SC_Trace(w3,scan,&error);
  253.  
  254.       /* Set event high when scan is positive */
  255.       SC_Event(event1,scan > 0.0 ? 1:-1, &error);
  256.       SC_Event(event2,scan > 0.0 ? 1:-1, &error);
  257.       SC_Event(event3,scan > 0.0 ? 1:-1, &error);
  258.  
  259.       SC_Draw();
  260.       SC_Advance(1.0/sps); /* next */
  261.    }
  262.  
  263.    SC_Close();
  264.    FreeMem();
  265. }
  266.  
  267.  
  268.